const express = require('express')

const app = express()

const appData = require('./data.json')
const { seller, goods, ratings } = appData

const router = express.Router()

router.get('/seller', (req, res) => {
    const id = req.query.id
    res.json({
        errno: 0,
        // 返回处理过的seller对象，并转化为json
        data: Object.assign({}, seller, {id})
    })
})

router.get('/goods', (req, res) => {
    res.json({
        errno: 0,
        data: goods
    })
})

router.get('/ratings', (req, res) => {
    res.json({
        errno: 0,
        data: ratings
    })
})

app.use('/api', router)
app.use(express.static('./dist'))

const port = process.env.PORT || 8900

module.exports = app.listen(port, (err) => {
    if (err) {
        console.log(err)
        return
    }
    console.log('listening at http://localhost:' + port + '\n')
})
